
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.dispatch.dispatcher &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-dispatch-dispatcher">
            
  <h1>Source code for django.dispatch.dispatcher</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">weakref</span>

<span class="kn">from</span> <span class="nn">django.utils.inspect</span> <span class="k">import</span> <span class="n">func_accepts_kwargs</span>


<span class="k">def</span> <span class="nf">_make_id</span><span class="p">(</span><span class="n">target</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="s1">&#39;__func__&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">target</span><span class="o">.</span><span class="vm">__self__</span><span class="p">),</span> <span class="nb">id</span><span class="p">(</span><span class="n">target</span><span class="o">.</span><span class="vm">__func__</span><span class="p">))</span>
    <span class="k">return</span> <span class="nb">id</span><span class="p">(</span><span class="n">target</span><span class="p">)</span>


<span class="n">NONE_ID</span> <span class="o">=</span> <span class="n">_make_id</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>

<span class="c1"># A marker for caching</span>
<span class="n">NO_RECEIVERS</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>


<div class="viewcode-block" id="Signal"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.Signal">[docs]</a><span class="k">class</span> <span class="nc">Signal</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for all signals</span>

<span class="sd">    Internal attributes:</span>

<span class="sd">        receivers</span>
<span class="sd">            { receiverkey (id) : weakref(receiver) }</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">providing_args</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">use_caching</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a new signal.</span>

<span class="sd">        providing_args</span>
<span class="sd">            A list of the arguments this signal can pass along in a send() call.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">providing_args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">providing_args</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">providing_args</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">providing_args</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_caching</span> <span class="o">=</span> <span class="n">use_caching</span>
        <span class="c1"># For convenience we create empty caches even if they are not used.</span>
        <span class="c1"># A note about caching: if use_caching is defined, then for each</span>
        <span class="c1"># distinct sender we cache the receivers that sender has in</span>
        <span class="c1"># &#39;sender_receivers_cache&#39;. The cache is cleaned when .connect() or</span>
        <span class="c1"># .disconnect() is called and populated on send().</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakKeyDictionary</span><span class="p">()</span> <span class="k">if</span> <span class="n">use_caching</span> <span class="k">else</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dead_receivers</span> <span class="o">=</span> <span class="kc">False</span>

<div class="viewcode-block" id="Signal.connect"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.Signal.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">receiver</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weak</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">dispatch_uid</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Connect receiver to sender for signal.</span>

<span class="sd">        Arguments:</span>

<span class="sd">            receiver</span>
<span class="sd">                A function or an instance method which is to receive signals.</span>
<span class="sd">                Receivers must be hashable objects.</span>

<span class="sd">                If weak is True, then receiver must be weak referenceable.</span>

<span class="sd">                Receivers must be able to accept keyword arguments.</span>

<span class="sd">                If a receiver is connected with a dispatch_uid argument, it</span>
<span class="sd">                will not be added if another receiver was already connected</span>
<span class="sd">                with that dispatch_uid.</span>

<span class="sd">            sender</span>
<span class="sd">                The sender to which the receiver should respond. Must either be</span>
<span class="sd">                a Python object, or None to receive events from any sender.</span>

<span class="sd">            weak</span>
<span class="sd">                Whether to use weak references to the receiver. By default, the</span>
<span class="sd">                module will attempt to use weak references to the receiver</span>
<span class="sd">                objects. If this parameter is false, then strong references will</span>
<span class="sd">                be used.</span>

<span class="sd">            dispatch_uid</span>
<span class="sd">                An identifier used to uniquely identify a particular instance of</span>
<span class="sd">                a receiver. This will usually be a string, though it may be</span>
<span class="sd">                anything hashable.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>

        <span class="c1"># If DEBUG is on, check that we got a good receiver</span>
        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">configured</span> <span class="ow">and</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">callable</span><span class="p">(</span><span class="n">receiver</span><span class="p">),</span> <span class="s2">&quot;Signal receivers must be callable.&quot;</span>

            <span class="c1"># Check for **kwargs</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">func_accepts_kwargs</span><span class="p">(</span><span class="n">receiver</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Signal receivers must accept keyword arguments (**kwargs).&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">dispatch_uid</span><span class="p">:</span>
            <span class="n">lookup_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">dispatch_uid</span><span class="p">,</span> <span class="n">_make_id</span><span class="p">(</span><span class="n">sender</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lookup_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">_make_id</span><span class="p">(</span><span class="n">receiver</span><span class="p">),</span> <span class="n">_make_id</span><span class="p">(</span><span class="n">sender</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">weak</span><span class="p">:</span>
            <span class="n">ref</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span>
            <span class="n">receiver_object</span> <span class="o">=</span> <span class="n">receiver</span>
            <span class="c1"># Check for bound methods</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">receiver</span><span class="p">,</span> <span class="s1">&#39;__self__&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">receiver</span><span class="p">,</span> <span class="s1">&#39;__func__&#39;</span><span class="p">):</span>
                <span class="n">ref</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakMethod</span>
                <span class="n">receiver_object</span> <span class="o">=</span> <span class="n">receiver</span><span class="o">.</span><span class="vm">__self__</span>
            <span class="n">receiver</span> <span class="o">=</span> <span class="n">ref</span><span class="p">(</span><span class="n">receiver</span><span class="p">)</span>
            <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">receiver_object</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_remove_receiver</span><span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_clear_dead_receivers</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">r_key</span> <span class="o">==</span> <span class="n">lookup_key</span> <span class="k">for</span> <span class="n">r_key</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">lookup_key</span><span class="p">,</span> <span class="n">receiver</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span></div>

<div class="viewcode-block" id="Signal.disconnect"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.Signal.disconnect">[docs]</a>    <span class="k">def</span> <span class="nf">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">receiver</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dispatch_uid</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Disconnect receiver from sender for signal.</span>

<span class="sd">        If weak references are used, disconnect need not be called. The receiver</span>
<span class="sd">        will be removed from dispatch automatically.</span>

<span class="sd">        Arguments:</span>

<span class="sd">            receiver</span>
<span class="sd">                The registered receiver to disconnect. May be none if</span>
<span class="sd">                dispatch_uid is specified.</span>

<span class="sd">            sender</span>
<span class="sd">                The registered sender to disconnect</span>

<span class="sd">            dispatch_uid</span>
<span class="sd">                the unique identifier of the receiver to disconnect</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">dispatch_uid</span><span class="p">:</span>
            <span class="n">lookup_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">dispatch_uid</span><span class="p">,</span> <span class="n">_make_id</span><span class="p">(</span><span class="n">sender</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lookup_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">_make_id</span><span class="p">(</span><span class="n">receiver</span><span class="p">),</span> <span class="n">_make_id</span><span class="p">(</span><span class="n">sender</span><span class="p">))</span>

        <span class="n">disconnected</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_clear_dead_receivers</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="p">)):</span>
                <span class="p">(</span><span class="n">r_key</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">r_key</span> <span class="o">==</span> <span class="n">lookup_key</span><span class="p">:</span>
                    <span class="n">disconnected</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
                    <span class="k">break</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">disconnected</span></div>

    <span class="k">def</span> <span class="nf">has_listeners</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_live_receivers</span><span class="p">(</span><span class="n">sender</span><span class="p">))</span>

<div class="viewcode-block" id="Signal.send"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.Signal.send">[docs]</a>    <span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Send signal from sender to all connected receivers.</span>

<span class="sd">        If any receiver raises an error, the error propagates back through send,</span>
<span class="sd">        terminating the dispatch loop. So it&#39;s possible that all receivers</span>
<span class="sd">        won&#39;t be called if an error is raised.</span>

<span class="sd">        Arguments:</span>

<span class="sd">            sender</span>
<span class="sd">                The sender of the signal. Either a specific object or None.</span>

<span class="sd">            named</span>
<span class="sd">                Named arguments which will be passed to receivers.</span>

<span class="sd">        Return a list of tuple pairs [(receiver, response), ... ].</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span> <span class="ow">is</span> <span class="n">NO_RECEIVERS</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">return</span> <span class="p">[</span>
            <span class="p">(</span><span class="n">receiver</span><span class="p">,</span> <span class="n">receiver</span><span class="p">(</span><span class="n">signal</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">receiver</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_live_receivers</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span>
        <span class="p">]</span></div>

<div class="viewcode-block" id="Signal.send_robust"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.Signal.send_robust">[docs]</a>    <span class="k">def</span> <span class="nf">send_robust</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Send signal from sender to all connected receivers catching errors.</span>

<span class="sd">        Arguments:</span>

<span class="sd">            sender</span>
<span class="sd">                The sender of the signal. Can be any Python object (normally one</span>
<span class="sd">                registered with a connect if you actually want something to</span>
<span class="sd">                occur).</span>

<span class="sd">            named</span>
<span class="sd">                Named arguments which will be passed to receivers. These</span>
<span class="sd">                arguments must be a subset of the argument names defined in</span>
<span class="sd">                providing_args.</span>

<span class="sd">        Return a list of tuple pairs [(receiver, response), ... ].</span>

<span class="sd">        If any receiver raises an error (specifically any subclass of</span>
<span class="sd">        Exception), return the error instance as the result for that receiver.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span> <span class="ow">is</span> <span class="n">NO_RECEIVERS</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="c1"># Call each receiver with whatever arguments it can accept.</span>
        <span class="c1"># Return a list of tuple pairs [(receiver, response), ... ].</span>
        <span class="n">responses</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">receiver</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_live_receivers</span><span class="p">(</span><span class="n">sender</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">response</span> <span class="o">=</span> <span class="n">receiver</span><span class="p">(</span><span class="n">signal</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="n">responses</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">receiver</span><span class="p">,</span> <span class="n">err</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">responses</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">receiver</span><span class="p">,</span> <span class="n">response</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">responses</span></div>

    <span class="k">def</span> <span class="nf">_clear_dead_receivers</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Note: caller is assumed to hold self.lock.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dead_receivers</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_dead_receivers</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">r</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span>
                <span class="k">if</span> <span class="ow">not</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ReferenceType</span><span class="p">)</span> <span class="ow">and</span> <span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">]()</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span>
            <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_live_receivers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Filter sequence of receivers to get resolved, live receivers.</span>

<span class="sd">        This checks for weak references and resolves them, then returning only</span>
<span class="sd">        live receivers.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">receivers</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_caching</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dead_receivers</span><span class="p">:</span>
            <span class="n">receivers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span>
            <span class="c1"># We could end up here with NO_RECEIVERS even if we do check this case in</span>
            <span class="c1"># .send() prior to calling _live_receivers() due to concurrent .send() call.</span>
            <span class="k">if</span> <span class="n">receivers</span> <span class="ow">is</span> <span class="n">NO_RECEIVERS</span><span class="p">:</span>
                <span class="k">return</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">receivers</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_clear_dead_receivers</span><span class="p">()</span>
                <span class="n">senderkey</span> <span class="o">=</span> <span class="n">_make_id</span><span class="p">(</span><span class="n">sender</span><span class="p">)</span>
                <span class="n">receivers</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="p">(</span><span class="n">receiverkey</span><span class="p">,</span> <span class="n">r_senderkey</span><span class="p">),</span> <span class="n">receiver</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">receivers</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">r_senderkey</span> <span class="o">==</span> <span class="n">NONE_ID</span> <span class="ow">or</span> <span class="n">r_senderkey</span> <span class="o">==</span> <span class="n">senderkey</span><span class="p">:</span>
                        <span class="n">receivers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">receiver</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_caching</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">receivers</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="p">[</span><span class="n">sender</span><span class="p">]</span> <span class="o">=</span> <span class="n">NO_RECEIVERS</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="c1"># Note, we must cache the weakref versions.</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">sender_receivers_cache</span><span class="p">[</span><span class="n">sender</span><span class="p">]</span> <span class="o">=</span> <span class="n">receivers</span>
        <span class="n">non_weak_receivers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">receiver</span> <span class="ow">in</span> <span class="n">receivers</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">receiver</span><span class="p">,</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ReferenceType</span><span class="p">):</span>
                <span class="c1"># Dereference the weak reference.</span>
                <span class="n">receiver</span> <span class="o">=</span> <span class="n">receiver</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">receiver</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">non_weak_receivers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">receiver</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">non_weak_receivers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">receiver</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">non_weak_receivers</span>

    <span class="k">def</span> <span class="nf">_remove_receiver</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">receiver</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Mark that the self.receivers list has dead weakrefs. If so, we will</span>
        <span class="c1"># clean those up in connect, disconnect and _live_receivers while</span>
        <span class="c1"># holding self.lock. Note that doing the cleanup here isn&#39;t a good</span>
        <span class="c1"># idea, _remove_receiver() will be called as side effect of garbage</span>
        <span class="c1"># collection, and so the call can happen while we are already holding</span>
        <span class="c1"># self.lock.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dead_receivers</span> <span class="o">=</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="receiver"><a class="viewcode-back" href="../../../topics/signals.html#django.contrib.auth.signals.receiver">[docs]</a><span class="k">def</span> <span class="nf">receiver</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A decorator for connecting receivers to signals. Used by passing in the</span>
<span class="sd">    signal (or list of signals) and keyword arguments to connect::</span>

<span class="sd">        @receiver(post_save, sender=MyModel)</span>
<span class="sd">        def signal_receiver(sender, **kwargs):</span>
<span class="sd">            ...</span>

<span class="sd">        @receiver([post_save, post_delete], sender=MyModel)</span>
<span class="sd">        def signals_receiver(sender, **kwargs):</span>
<span class="sd">            ...</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">_decorator</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">signal</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">signal</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">func</span>
    <span class="k">return</span> <span class="n">_decorator</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>